<template>
    <div class="patientScreen-view">

      <template v-if="user.data_scope == 3 || !user.department || !user.data_scope">

        <div class="screen-content">
          <div class="screen-item">
            <span class="screen-title">患者姓名</span>
            <el-input v-model="data.search" placeholder="患者姓名"></el-input>
          </div>

          <div class="screen-item">
            <span class="screen-title">选择医生</span>
            <el-select v-model="data.docValue" @change="docChange" placeholder="请选择医生">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.real_name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>

          <div class="screen-item">
            <span class="screen-title">选择科室</span>
            <el-select v-model="data.departmentValue" @change="departmentChange" placeholder="请选择科室">
              <el-option
                v-for="item in departments"
                :key="item.id"
                :label="item.department_name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>

          <el-button class="main-btn" type="primary" @click="searchClick">查询</el-button>
          <el-button class="border-btn" @click="clearData">重置</el-button>

          <a v-if="type > 2" class="morBtn" @click="moreClick">更多筛选 >></a>
        </div>

        <div v-if="isMore" class="screen-content" style="margin-top: 10px">
          <div class="screen-item" v-if="type == 3 || type == 4">
            <span class="screen-title">选择日期</span>
            <el-date-picker
              class="mouthDate"
              v-model="day"
              type="date"
              @change="dayChange"
              placeholder="选择日期"
              :picker-options="type == 3 ? pickerOptions : pickerOptions1">
            </el-date-picker>
          </div>

          <div class="screen-item" v-if="type == 4">
            <span class="screen-title">选择月份</span>
            <el-date-picker
              v-model="month"
              type="month"
              @change="monthChange"
              :picker-options="monthPickerOptions"
              placeholder="选择月">
            </el-date-picker>
          </div>

          <div class="screen-item" v-if="type == 5">
            <span class="screen-title">选择年份</span>
            <el-date-picker
              v-model="year"
              type="year"
              @change="yearChange"
              :picker-options="yearPickerOptions"
              placeholder="选择年">
            </el-date-picker>
          </div>

          <div class="screen-item" v-if="type == 5">
            <span class="screen-title">选择时间</span>
            <el-date-picker
              v-model="valueDate"
              type="daterange"
              @change="dateChange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </div>

      </template>

      <template v-else-if="user.data_scope == 2 || user.data_scope == 1">
        <div class="screen-content" >

          <div class="screen-item">
            <span class="screen-title">患者姓名</span>
            <el-input v-model="data.search" placeholder="患者姓名"></el-input>
          </div>

          <div class="screen-item" v-if="user.data_scope == 2">
            <span class="screen-title">选择医生</span>
            <el-select v-model="data.docValue" @change="docChange" placeholder="请选择医生">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.real_name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>

          <div class="screen-item" v-if="type == 3 || type == 4">
            <span class="screen-title">选择日期</span>
            <el-date-picker
              class="mouthDate"
              v-model="day"
              type="date"
              @change="dayChange"
              placeholder="选择日期"
              :picker-options="type == 3 ? pickerOptions : pickerOptions1">
            </el-date-picker>
          </div>

          <div class="screen-item" v-if="type == 4">
            <span class="screen-title">选择月</span>
            <el-date-picker
              v-model="month"
              type="month"
              @change="monthChange"
              :picker-options="monthPickerOptions"
              placeholder="选择月">
            </el-date-picker>
          </div>

          <div class="screen-item" v-if="type == 5">
            <span class="screen-title">选择年</span>
            <el-date-picker
              v-model="year"
              type="year"
              @change="yearChange"
              :picker-options="yearPickerOptions"
              placeholder="选择年">
            </el-date-picker>
          </div>

          <div class="screen-item" v-if="type == 5">
            <span class="screen-title">选择时间</span>
            <el-date-picker
              v-model="valueDate"
              type="daterange"
              range-separator="至"
              @change="dateChange"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </div>

          <el-button class="main-btn" type="primary" @click="searchClick">查询</el-button>
          <el-button class="border-btn" @click="clearData">重置</el-button>

        </div>
      </template>



    </div>
</template>

<script>
    import {getDoctorment,getDepartment} from "../../../api/registration";

    export default {
        name: "screenView",
        props:{
            data:Object,
            type:Number,
        },
        model: {
            prop: 'data',
            event: 'change'
        },
        data:function () {
            return{
                user:{},
                options: [],
                departments:[],
                day:'',
                month:'',
                year:'',
                valueDate:[],
                pickerOptions: {
                    disabledDate(time) {
                        var now = new Date(); //当前日期
                        var nowMonth = now.getMonth(); //当前月
                        var nowYear = now.getFullYear(); //当前年
                        //本月的开始时间
                        var monthStartDate = new Date(nowYear, nowMonth, 1);
                        if (time.getTime() >= monthStartDate && time.getTime() < Date.now()){
                            return false;
                        }else {
                            return true;
                        }

                        // return time.getTime() > Date.now();
                    }
                },
                pickerOptions1: {
                    disabledDate(time) {
                        var now = new Date(); //当前日期
                        var nowMonth = now.getMonth(); //当前月
                        var nowYear = now.getFullYear(); //当前年
                        //本月的开始时间
                        var monthStartDate = new Date(nowYear, 0, 1);
                        if (time.getTime() >= monthStartDate && time.getTime() < Date.now()){
                            return false;
                        }else {
                            return true;
                        }

                        // return time.getTime() > Date.now();
                    }
                },
                monthPickerOptions:{
                    disabledDate(time) {
                        var now = new Date(); //当前日期
                        var nowMonth = now.getMonth(); //当前月
                        var nowYear = now.getFullYear(); //当前年
                        //本月的开始时间
                        var monthStartDate = new Date(nowYear, 0, 1);
                        if (time.getTime() >= monthStartDate && time.getTime() < Date.now()){
                            return false;
                        }else {
                            return true;
                        }

                        // return time.getTime() > Date.now();
                    }
                },
                yearPickerOptions:{
                    disabledDate(time) {
                        var now = new Date(); //当前日期
                        var nowMonth = now.getMonth(); //当前月
                        var nowYear = now.getFullYear(); //当前年
                        //本月的开始时间
                        var monthStartDate = new Date(1900, 0, 1);
                        if (time.getTime() >= monthStartDate && time.getTime() < Date.now()){
                            return false;
                        }else {
                            return true;
                        }

                        // return time.getTime() > Date.now();
                    }
                },
                isMore:false,
            }
        },
        mounted:function() {
            this.user = this.$store.getters.userInfo

            this.getDoctorment();
            this.getDepartment();
        },
        methods:{
            moreClick:function(){
                this.isMore = !this.isMore;
                this.$emit("more-click")
            },
            getDoctorment:function(){
                let data = {};
                if (this.user.data_scope == 2 && this.user.department){
                    data.id = this.user.department.ID
                }
                getDoctorment(data).then((res)=>{
                    if (res.code == 200){
                        this.options = res.data;
                        if (this.options.length > 0){
                            this.options.unshift({id:0,real_name:'全部'})
                        }else {
                            this.data.docValue = "";
                        }
                    }else {
                        this.$message.error(res.msg);
                    }
                })
            },
            getDepartment:function(){
                let data = {
                    pager: {},
                    key_word: ''
                }
                getDepartment(data).then((res)=>{
                    if (res.code == 200){
                        this.departments = res.data.data;
                        if (this.departments.length > 0){
                            this.departments.unshift({id:0,department_name:'全部'})
                        }else {
                            this.data.departmentValue = "";
                        }
                    }else {
                        this.$message.error(res.msg);
                    }
                }).catch((err)=>{

                })
            },
            searchClick:function () {
                this.$emit("screen-data")
            },
            clearData:function () {
                this.data.search = "";
                this.data.departmentValue = this.options.length > 0 ? 0 :'';
                this.data.docValue = this.departments.length > 0 ? 0 :'';
                this.month = "";
                this.day = "";
                this.year = "";
                this.valueDate = []

                if (this.type == 3){
                    let end = new Date();
                    let start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }else if (this.type == 4){
                    let end = new Date();
                    let start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }else if (this.type == 5){
                    this.data.startDate = "";
                    this.data.endDate = "";
                }

                this.$emit("screen-data")
            },
            docChange:function(){
                this.data.departmentValue = 0;
                this.$emit("screen-data")
                // this.pageNum = 1;
                // this.getData()
            },
            departmentChange:function(){
                this.data.docValue = 0;
                this.$emit("screen-data")
                // this.pageNum = 1;
                // this.getData()
            },
            dateChange:function (val) {
                this.year = "";
                this.data.startDate = this.dealDate(this.valueDate[0]) + " 00:00:00";
                this.data.endDate = this.dealDate(this.valueDate[1]) + " 23:59:59"

                this.$emit("screen-data")
            },
            dayChange:function(val){
                this.month = ""

                if (val){
                    let start = val;

                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(start) + " 23:59:59";
                }else if (this.type == 3){
                    let end = new Date();
                    let start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }else if (this.type == 4){
                    let end = new Date();
                    let start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }

                this.$emit("screen-data")
            },
            yearChange:function (val) {
                this.valueDate = [];
                if (val){
                    const start = val;
                    const end = new Date();

                    let day = 364
                    if (this.isLeap(start.getFullYear())){
                        day = 365
                    }
                    end.setTime(start.getTime() + 3600 * 1000 * 24 * day);

                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }else {
                    this.data.startDate = "";
                    this.data.endDate = "";
                }

                this.$emit("screen-data")
            },
            monthChange:function (val,val1) {
                this.day = "";
                if (val){
                    let start = val;
                    let end = new Date();

                    let day = "";
                    switch (start.getMonth() + 1) {
                        case 1:
                            day = 30;
                            break;
                        case 2:
                            day = this.isLeap(start.getFullYear()) ? 28 :27;
                            break;
                        case 3:
                            day = 30;
                            break;
                        case 4:
                            day = 29;
                            break;
                        case 5:
                            day = 30;
                            break;
                        case 6:
                            day = 29;
                            break;
                        case 7:
                            day = 30;
                            break;
                        case 8:
                            day = 30;
                            break;
                        case 9:
                            day = 29;
                            break;
                        case 10:
                            day = 30;
                            break;
                        case 11:
                            day = 29;
                            break;
                        case 12:
                            day = 30;
                            break;
                        default:
                            day = 29;
                            break;
                    }

                    end.setTime(start.getTime() + 3600 * 1000 * 24 * day);
                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }else {
                    let end = new Date();
                    let start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
                    this.data.startDate = this.dealDate(start) + " 00:00:00";
                    this.data.endDate = this.dealDate(end) + " 23:59:59";
                }

                this.$emit("screen-data")
            },
            isLeap:function (year) {
                if((year%4==0 && year%100!=0)||(year%400==0)){
                    return 1;
                }
                return 0;
            },
            dealDate:function (date) {
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();
                if (month < 10){
                    month = "0" + month
                }
                if (day < 10){
                    day = "0" + day;
                }

                return year + "-" + month + "-" + day
            },
        }
    }
</script>

<style lang="scss">
  @import "@/assets/scss/patient/screenView.scss";
</style>
